package top.riske.gen3d.common.config;

import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.riske.gen3d.http.JwtDtoArgumentResolver;


import java.util.List;

/**
 * created by vital on 2025/3/5 下午5:27
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private JwtDtoArgumentResolver jwtDtoArgumentResolver;

    @Value("${gen3d.file.path}")
    private String filePath;

    @Value("${gen3d.file.root.path}")
    private String fileRootPath;

    /**
     * 添加JWT参数解析器
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(jwtDtoArgumentResolver);
    }

    /**
     * 静态资源映射，将 /file/ 映射到指定的文件路径
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将 /file/ 映射到指定的文件路径
        registry.addResourceHandler(fileRootPath+"/**")
                .addResourceLocations("file:"+filePath);
    }

    /**
     * 解决跨域
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") //允许所有路径跨域
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许所有请求方法
                .allowedHeaders("*") // 允许所有请求头
                .allowedOriginPatterns("*") // 支持域
                .exposedHeaders("*"); // 暴露头
        WebMvcConfigurer.super.addCorsMappings(registry);
    }
}
